<template>
	<view>
		<u-navbar title="添加车辆" bgColor="#3083FB" titleStyle="color:#fff" left-icon-color="#fff" :autoBack="true"
			placeholder></u-navbar>
		<view class="bg"></view>

		<view class="main">
			<view class="content">
				<view class="pic">
					<image src="/static/images/add-car.png" mode="aspectFill"></image>
				</view>
				<view class="form">
					<u--form labelPosition="left" :model="model1" ref="uForm">
						<u-form-item label="车辆类型" label-width="180rpx" @click="showType = true;">
							<u--input v-model="model1.car.cartype" disabled disabledColor="#ffffff" placeholder="请选择车辆类型"
								border="none"></u--input>
						</u-form-item>
						<u-form-item label="车辆型号" label-width="180rpx" @click="showMsg = true;">
							<u--input v-model="model1.car.carmsg" disabled disabledColor="#ffffff" placeholder="请选择车辆型号"
								border="none"></u--input>
						</u-form-item>


						<u-form-item label="车牌号码" label-width="180rpx" @click="cardBord">
							<u--input v-model="model1.car.num" border="none" placeholder="请输入车牌号码" readonly ></u--input>
						</u-form-item>

						<u-form-item label="车主姓名" label-width="180rpx">
							<u--input v-model="model1.car.name" border="none" placeholder="请输入车主姓名"></u--input>
						</u-form-item>
						<u-form-item label="电话号码" label-width="180rpx">
							<u--input v-model="model1.car.phone" border="none" placeholder="请输入电话号码"></u--input>
						</u-form-item>


						<u-form-item label="照片" label-width="180rpx">
							<u-upload :fileList="fileList1" width="150rpx" height="150rpx" @afterRead="afterRead" @delete="deletePic"
								name="1" multiple :maxCount="10">
								<view class="box">
									<image src="/static/images/plus-car.png" mode="widthFix" style="width: 70rpx;height: 70px;"></image>
									<text>上传车辆照片</text>
								</view>
							</u-upload>
						</u-form-item>


					</u--form>

					<u-action-sheet :show="showType" :actions="carTypeActions" title="请选择车辆类型" @close="showType = false"
						@select="typeSelect">
					</u-action-sheet>

					<u-action-sheet :show="showMsg" :actions="carMsgActions" title="请选择车辆型号" @close="showMsg = false"
						@select="msgSelect">
					</u-action-sheet>
				</view>
			</view>
		</view>

		<view class="addCar">
			<u-button type="primary" @click="onAddCar">
				<u-icon name="plus" color="#fff" size="14"></u-icon>
				<text class="text">添加车辆</text>
			</u-button>
		</view>
		<u-keyboard :mode="keyData.mode" :dotDisabled="keyData.dotDisabled" :overlay="false" :random='keyData.random' :show="showKeyBoard"
			@close="close" @cancel="cancel" @confirm="confirm" @change="change" @backspace="backspace"></u-keyboard>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 车牌键盘

				keyData: {
					mode: '',
					dotDisabled: false,
					random: false,
				},
				showKeyBoard: false,


				fileList1: [],
				showType: false,
				showMsg: false,
				model1: {
					car: {
						cartype: '',
						carmsg: '',
						num: '',
						name: '',
						phone: '',
					},
				},
				carTypeActions: [{
					name: '小客车'
				}, {
					name: '其他客车'
				}, {
					name: '微型客车'
				}, {
					name: '轻型客车'
				}, {
					name: '中型客车'
				}, {
					name: '重型客车'
				}],
				carMsgActions: [{
					name: '大众'
				}, {
					name: '奔驰'
				}, {
					name: '宝马'
				}, {
					name: '奥迪'
				}, {
					name: '其他'
				}],
				radio: '',
				timer:'',
			};
		},
		methods: {
			typeSelect(e) {
				this.model1.car.cartype = e.name
			},
			msgSelect(e) {
				this.model1.car.carmsg = e.name
			},



			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},

			onAddCar() {
				uni.redirectTo({
					url: '/pages/My/MyCar/AddSuccess'
				})
			},
			change(e) {
				// console.log('change');
				this.model1.car.num += e;
			},
			cardBord() {
				this.showKeyBoard = true;
			},
			close() {
				// console.log('close');
				this.showKeyBoard = false
			},
			cancel() {
				// console.log('cancel');
				this.showKeyBoard = false
			},
			confirm() {
				// console.log('confirm');
				this.showKeyBoard = false
			},
			backspace() {
				this.model1.car.num = this.model1.car.num.slice(0, -1)
			}
		},
	}
</script>

<style lang="scss">
	.bg {
		background-color: #3083FB;
		height: 200rpx;
		margin-top: -2px;
	}

	.main {
		padding: 20rpx;
		padding-top: 30rpx;
		margin-top: -190rpx;

		.content {
			background-color: #fff;
			width: 100%;
			height: 300rpx;
			border-radius: 18rpx;

			.pic {
				width: 400rpx;
				height: 300rpx;
				margin: 0 auto;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.form {
				background-color: #fff;
				padding: 0 30rpx 150rpx;

				/deep/ .u-form-item__body__left__content__label {
					color: #fff;
					background-color: #3083FB;
					padding: 20rpx;
					justify-content: center !important;
					border-radius: 50rpx;
				}

				/deep/ .u-input__content__field-wrapper__field {
					border: 1px solid #E5E5E5;
					border-radius: 50rpx;
					padding: 10rpx 20rpx;
				}

				.box {
					width: 150rpx;
					height: 150rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					border: 1px solid #BFBFBF;
					border-radius: 8rpx;
					flex-direction: column;

					image {
						width: 30rpx !important;
						margin-bottom: 10rpx;
					}

					text {
						font-size: 24rpx;
						color: #BFBFBF;
					}
				}
			}
		}
	}

	// 添加按钮
	.addCar {
		width: 750rpx;
		box-sizing: border-box;
		position: fixed;
		bottom: 40rpx;
		padding: 0 20rpx;

		.text {
			margin-left: 20rpx;
		}

		/deep/ .u-button {
			background: linear-gradient(to right, #3789FF, #0362E6);
			border-radius: 40rpx !important;
		}
	}
</style>